package com.zlq.login.controller;

import com.zlq.login.dao.UserDaoLo;
import com.zlq.login.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/listByPageController")
public class ListByPageController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=Utf-8");

        UserDaoLo ud = new UserDaoLo();

        int pagenum = Integer.parseInt(req.getParameter("pagenum"));
        int size = req.getParameter("size")==null?3:Integer.parseInt(req.getParameter("size"));

        List<User> users = ud.selectByPage(pagenum, size);
        Integer total = ud.selectcount();
        Integer to= (total/size);

        HttpSession session = req.getSession();
        session.setAttribute("to",to);

        session.setAttribute("users", users);
        session.setAttribute("total", total);
        session.setAttribute("pagenum",pagenum);
//        req.getRequestDispatcher("/static/listUser.jsp").forward(req,resp);

        resp.sendRedirect("/static/listUser.jsp");

    }
}
